package dao.impl;/**
 * @author V&X
 * @date 2021/5/26 19:50
 * @version 1.0
 */

import dao.ICountryDao;
import dao.IProvinceDao;
import dao.IUserDao;
import dao.IUserLevelDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.*;

import java.math.BigInteger;
import java.security.Provider;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class UserDaoImpl implements IUserDao {
    private  ICountryDao countryDao = new CountryDaoImpl();
    private  IProvinceDao provinceDao = new ProvinceDaoImpl();
    private  IUserLevelDao userLevelDao = new UserLevelDaoImpl();

    @Override
    public void addUser(User user) {
        QueryRunner qr = null;
        qr = new QueryRunner();
        Connection connection = null;
        try {
            connection = FirstApp.getConn();
            //自动提交事务关闭
            connection.setAutoCommit(false);
            String sql = "insert into user(username,password) values(?,?)";
            //userinfo
            String infosql = "insert into userinfo(countryid,userid,provinceid,userlevelid) values(?,?,?,?)";
            String catSql = "insert into shopcart(userid) values(?)";
            //获取当前插入用户id
            BigInteger integer = qr.insert(connection, sql, new ScalarHandler<>(1), user.getUsername(), user.getPassword());
            System.out.println(integer);
            qr.update(connection, catSql, integer);
            //获得info里面的三个对象
            Country country = countryDao.findByCountryName(user.getUserInfo().getCountry().getCountryName());
            Province province = provinceDao.findByProvinceName(user.getUserInfo().getProvince().getProvinceName());
            qr.update(connection, infosql, country.getCountryid(), integer, province.getProvinceid(), 6);
            connection.commit();//开启事务
            //设置异常
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                System.out.println("================");
                //事变： 回滚事务
                connection.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                DbUtils.close(connection);
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public void updateUser(User user) {
        QueryRunner qr = new QueryRunner();
        Connection connection = null;
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "update user set password=? where id =?";
            qr.update(connection, sql, user.getPassword(), user.getId());
            String sqlUserinfo = "update userinfo set countryid = ?,provinceid = ? where userid = ?";
            Country country = countryDao.findByCountryName(user.getUserInfo().getCountry().getCountryName());
            Province province = provinceDao.findByProvinceName(user.getUserInfo().getProvince().getProvinceName());
            qr.update(connection, sqlUserinfo, country.getCountryid(), province.getProvinceid(), user.getId());
            connection.commit();
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } finally {
            try {
                DbUtils.close(connection);
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public void deleteUser(User user) {
        QueryRunner qr = new QueryRunner();
        Connection connection = null;
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "delete from user where id=?";
            String infoSql = "delete from userinfo where userid=?";
            String CarSql = "delete from shopcart  where userid=?";
            qr.update(connection, sql, user.getId());
            qr.update(connection, infoSql, user.getId());
            qr.update(connection, CarSql, user.getId());
            connection.commit();
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } finally {
            try {
                DbUtils.close(connection);
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public User findByNameAnaPassword(String name, String password) {
        User user = null;
        QueryRunner qr = null;
        try {
            String sql = "Select * from user where username=? and password=?";
            qr = new QueryRunner();
            user = qr.query(FirstApp.getConn(), sql, new BeanHandler<>(User.class), name, password);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public User findByid(int id) {

        User user = null;
        UserInfo userInfo = null;
        QueryRunner qr = null;
        try {
            String sql = "select * from user where id=?";
            String infosql ="select * from userinfo where userid=?";
            String countrysql ="select countryid from userinfo where userid=?";
            String provincesql ="select provinceid from userinfo where userid=?";
            String  userlevelsql ="select userlevelid from userinfo where userid=?";
            qr = new QueryRunner();
            user = qr.query(FirstApp.getConn(), sql, new BeanHandler<>(User.class), id);
            userInfo = qr.query(FirstApp.getConn(), infosql, new BeanHandler<>(UserInfo.class), user.getId());
            int countryid = qr.query(FirstApp.getConn(), countrysql,new ScalarHandler<>(1), user.getId());
            int  provinceid= qr.query(FirstApp.getConn(), provincesql,new ScalarHandler<>(1), user.getId());
            int userlevelid = qr.query(FirstApp.getConn(), userlevelsql,new ScalarHandler<>(1), user.getId());
            System.out.println("--------"+countryid+"--------"+provinceid+"--------"+userlevelid);
            Country country = countryDao.findById(countryid);
            Province province = provinceDao.findById(provinceid);
            UserLevel userLevel = userLevelDao.findByid(userlevelid);
            userInfo.setCountry(country);
            userInfo.setProvince(province);
            userInfo.setUser(user);
            userInfo.setUserLevel(userLevel);
            System.out.println(userInfo);
            user.setUserInfo(userInfo);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> userList = null;
        QueryRunner qr = null;
        try {
            String sql = "Select * from user";
            qr = new QueryRunner();
            userList = qr.query(FirstApp.getConn(), sql, new BeanListHandler<User>(User.class));
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return userList;
    }

    @Override
    public List<User> findLinkName(String name) {
        List<User> userList = null;
        QueryRunner qr = null;
        name = "%" + name + "%";
        try {
            String sql = "Select * from user where username like ?";
            qr = new QueryRunner();
            userList = qr.query(FirstApp.getConn(), sql, new BeanListHandler<>(User.class), name);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return userList;
    }
}
